Component({
    //todo:按要求，口味信息应该从数据库中获取，并且在order页面中读出，传递到popup-order组件中
    data: {
        hotsValue: 0,
        hots: [{
                name: "不辣",
                value: 1
            },
            {
                name: "微辣",
                value: 2
            },
            {
                name: "中辣",
                value: 3
            },
            {
                name: "特辣",
                value: 4
            },
        ],
        tastes: [{
            name: "免葱",
            flag: false
        }, {
            name: "免姜",
            flag: false
        }, {
            name: "免香菜",
            flag: false
        }, {
            name: "免酱油",
            flag: false
        }],
        content: ''
    },
    methods: {
        radioClick(event) {
            let value = event.currentTarget.dataset.value;
            this.setData({
                hotsValue: value
            });
            let temp = this.data.content;
            temp = temp.replace("不辣", '').replace("微辣", '').replace("中辣", '').replace('特辣', '');
            if (temp == '') {
                temp += this.data.hots[value - 1].name;
            } else {
                temp += "，";
                temp += this.data.hots[value - 1].name;
            }
            temp = this.regOperation(temp);
            this.setData({
                content: temp
            });
        },
        tasteClick(event) {
            let value = event.currentTarget.dataset.value;
            console.log(value);
            let tempContent = this.data.content;
            let temp = this.data.tastes;
            temp[value].flag = !temp[value].flag;
            if(temp[value].flag){
                if (tempContent == '') {
                    tempContent += this.data.tastes[value].name;
                } else {
                    tempContent += "，";
                    tempContent += this.data.tastes[value].name;
                }
            }else{
                tempContent = tempContent.replace(temp[value].name,'');
            }
            tempContent =this.regOperation(tempContent);
            this.setData({
                tastes:temp,
                content: tempContent
            });
        },
        switch () {
            this.setData({
                show: !this.data.show
            });
        },
        saveAndClose() {
            let result = {
                content: this.data.content
            };
            this.triggerEvent("closePopup", result); //触发一个事件，事件的名字是：closePopup。
            this.switch();
        },
        regOperation(tempContent){
            //利用正则表达式，去除增删操作中可能出现的连在一起的逗号，前缀逗号和后缀逗号
            let reg0 = /[\s]*[，][\s]*[，]{1,}([\s]+|[，]+)*/g; //连在一起的逗号
            let reg1 = /^[，]*[\s]*/g;        //前缀逗号
            let reg2 = /[\s]*[，]*$/g;        //后缀逗号
            tempContent =tempContent.replace(reg0,'，').replace(reg1,'').replace(reg2,'');
            return tempContent;
        }
    }
});